require('babel-register');
const Koa = require('koa');
const path = require('path');
const render = require('koa-art-template');
const routers = require('./routes/routers');
const serve = require('koa-static'); //静态资源
const bodyparser = require('koa-bodyparser');
const logger = require('koa-logger');
const app = new Koa();

app.use(bodyparser({
    enableTypes: ['json', 'form', 'text']
}))

app.use(logger())


render(app, {
    root: path.join(__dirname, 'views'),
    imports: { //过滤器
        dateFilter: (val, fmt) => {
            return val + 1
        }
    }
})
app.use(serve(path.join(__dirname)))

routers(app);

//logger
app.use(async (ctx, next) => {
    const start = Date.now();
    next();
    const ms = Date.now() - start;
    console.log(`method:${ctx.method} url:${ctx.url} ${ms}ms`);
})

// error-handling
app.on('error', (err, ctx) => {
    console.error('server error', err, ctx)
});

app.listen(3000);
